// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/* 引入Axios */
import axios from 'axios'

//配置axios请求的默认url
axios.defaults.baseURL = "http://localhost:7600/"

// 创建axios对象
let http = axios.create({});
// 配置拦截器
// 请求拦截器：在请求发出去之前执行一些特殊的代码，对所有的请求有效
http.interceptors.request.use(
  config => {
    // 统一设置信息：token
    //console.log("请求拦截器执行拦截.....");

    let token = window.localStorage.getItem("token");

    if (token) {
      // 将token放到请求里面
      config.headers.authorization = token;
    }

    // 返回配置对象，发送请求
    return config;
  }
)
// 响应拦截器，如果服务器响应头包含token信息，拦截器会自动存放到本地
http.interceptors.response.use(
  res => {
   // console.log("响应拦截器执行....");

    // 获得响应头的token放到本地
    let token = res.headers.authorization;

    if (token) {
      window.localStorage.setItem("token", token);
    }

    // 放行
    return res;
  }
)
Vue.use(ElementUI)

Vue.config.productionTip = false


// 将axios设置为Vue的元属性
Vue.prototype.$axios = http

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
